package com.hy.frame.widget

import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.hy.frame.R


/**
 * title 自动按比例缩放
 * author heyan
 * time 19-1-21 下午12:12
 * desc 无
 */
class AutoTextView : AppCompatTextView {

    //缩放比例
    private var ratio = 1f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.AutoTextView, defStyleAttr, 0)
        val ratioWidth = a.getInt(R.styleable.AutoTextView_atvRatioWidth, context.resources.displayMetrics.widthPixels)
        ratio = context.resources.displayMetrics.widthPixels.toFloat() / ratioWidth
        val padding = a.getInt(R.styleable.AutoTextView_atvPadding, 0)
        var paddingLeft = a.getInt(R.styleable.AutoTextView_atvPaddingLeft, padding)
        var paddingTop = a.getInt(R.styleable.AutoTextView_atvPaddingTop, padding)
        var paddingRight = a.getInt(R.styleable.AutoTextView_atvPaddingRight, padding)
        var paddingBottom = a.getInt(R.styleable.AutoTextView_atvPaddingBottom, padding)
        a.recycle()
        paddingLeft = getRatioSize(paddingLeft)
        paddingTop = getRatioSize(paddingTop)
        paddingRight = getRatioSize(paddingRight)
        paddingBottom = getRatioSize(paddingBottom)
        setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
    }


    private fun getRatioSize(number: Int): Int {
        return (number.toFloat() * ratio).toInt()
    }
}